package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.YanShouJiChaBean;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class JiChaDetailsActivity extends BaseActivity implements View.OnClickListener {
    private LinearLayout max_status1;
    private RecyclerView jindu;
    private YanShouJiChaBean bean = new YanShouJiChaBean();
    private TextView project_name, hetonghao, name, address, text1, text2;
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ji_cha_details);
        text1 = (TextView) findViewById(R.id.text1);
        text2 = (TextView) findViewById(R.id.text2);
        bean = (YanShouJiChaBean) getIntent().getSerializableExtra("bean");
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("稽查");
        TextView submit = (TextView) findViewById(R.id.submit);
        submit.setText("稽查");
        submit.setVisibility(View.VISIBLE);
        submit.setOnClickListener(this);


        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.status1).setOnClickListener(this);
        findViewById(R.id.status2).setOnClickListener(this);
        findViewById(R.id.jindu586).setOnClickListener(this);
        max_status1 = (LinearLayout) findViewById(R.id.max_status1);
        jindu = (RecyclerView) findViewById(R.id.jindu);
        jindu.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        project_name = (TextView) findViewById(R.id.project_name);
        project_name.setText(bean.getInfo().getProject_name());
        hetonghao = (TextView) findViewById(R.id.hetonghao);
        hetonghao.setText("合同编号：" + bean.getInfo().getBudget_no());
        name = (TextView) findViewById(R.id.name);
        name.setText(bean.getInfo().getRealname() + "\t  " + bean.getInfo().getTel());
        address = (TextView) findViewById(R.id.address);
        address.setText(bean.getInfo().getAddress());
//
        recycler = (RecyclerView) findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        final List<YanShouJiChaBean.InfoEntity.PackageEntity> packageX = bean.getInfo().getPackageX();
        recycler.setAdapter(new BaseRVAdapter(this, packageX) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou_fa3;
            }

            @Override
            public void onBind(BaseViewHolder holder, int position) {
                ImageLoader.getInstance().displayImage(packageX.get(position).getImg(), holder.getImageView(R.id.img));
                holder.setTextView(R.id.sname, packageX.get(position).getName());
                holder.setTextView(R.id.ss, packageX.get(position).getLabel());
                holder.setTextView(R.id.ss2, packageX.get(position).getInfo());
                holder.setTextView(R.id.numbs, packageX.get(position).getRoom_number());
                holder.setTextView(R.id.info, "×" + packageX.get(position).getNums());
            }

        });

        YanShouJiChaBean.InfoEntity.DetailEntity detail = bean.getInfo().getDetail();
        TextView install_time = (TextView) findViewById(R.id.install_time);
        install_time.setText(detail.getInstall_time());
        TextView send_require = (TextView) findViewById(R.id.send_require);
        send_require.setText(detail.getSend_require());
        TextView remark = (TextView) findViewById(R.id.remark);
        remark.setText(detail.getRemark());
        //
        final List<YanShouJiChaBean.InfoEntity.DetailEntity.ListEntity> list = detail.getList();
        RecyclerView home = (RecyclerView) findViewById(R.id.home);
        home.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        home.setAdapter(new BaseRVAdapter(this, list) {

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou_home;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.getView(R.id.pinglun).setVisibility(View.GONE);
                x.image().bind(holder.getImageView(R.id.tou), list.get(position).getHeadimg());
                holder.setTextView(R.id.name, list.get(position).getRealname());
                holder.setTextView(R.id.yanshoumingzi, list.get(position).getLevel());
                holder.setTextView(R.id.company, list.get(position).getCompany());
                holder.setTextView(R.id.address, list.get(position).getAddress());
                holder.setTextView(R.id.sj, list.get(position).getStart_time());
                holder.setTextView(R.id.info, list.get(position).getInfo());
                holder.setTextView(R.id.hao, "合同号：" + list.get(position).getBudget_name());
                RecyclerView recycler = holder.getView(R.id.recycler);
                final ImageView img = holder.getImageView(R.id.img);

                if (list.get(position).getImg() != null && list.get(position).getImg().size() > 1) {
                    recycler.setVisibility(View.VISIBLE);
                    img.setVisibility(View.GONE);
                    recycler.setLayoutManager(new GridLayoutManager(JiChaDetailsActivity.this, 3, LinearLayoutManager.VERTICAL, false));
                    recycler.setAdapter(new BaseRVAdapter(JiChaDetailsActivity.this, list.get(position).getImg()) {
                        @Override
                        public int getLayoutId(int viewType) {
                            return R.layout.item_img_recycler3;
                        }

                        @Override
                        public void onBind(BaseViewHolder holder, final int position2) {
                            com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(list.get(position).getImg().get(position2), holder.getImageView(R.id.img));


                            //  x.image().bind(holder.getImageView(R.id.img), info1.get(position).getImg().get(position2));
                            holder.getView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(JiChaDetailsActivity.this, ShowImageDetail.class);
                                    intent.putStringArrayListExtra("paths", (ArrayList<String>) list.get(position).getImg());
                                    intent.putExtra("index", position2);
                                    startActivity(intent);
                                }
                            });
                        }
                    });
                } else if (list.get(position).getImg() != null && list.get(position).getImg().size() == 1) {
                    img.setVisibility(View.VISIBLE);
                    recycler.setVisibility(View.GONE);
                    //    x.image().bind(holder.getImageView(R.id.img), info1.get(position).getImg().get(0));
                    com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(list.get(position).getImg().get(0), holder.getImageView(R.id.img));

                    // ImageLoadUrl.downloadImgByUrl(info1.get(position).getImg().get(0), holder.getImageView(R.id.img));
                } else {
                    img.setVisibility(View.GONE);
                    recycler.setVisibility(View.GONE);
                }
                img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(JiChaDetailsActivity.this, ShowImageDetail.class);
                        intent.putStringArrayListExtra("paths", (ArrayList<String>) list.get(position).getImg());
                        intent.putExtra("index", 0);
                        startActivity(intent);
                    }
                });


            }
        });

        final List<YanShouJiChaBean.InfoEntity.InstallEntity> install = bean.getInfo().getInstall();
        jindu.setAdapter(new BaseRVAdapter(this, install) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_wuliu;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                RecyclerView recyclerView = holder.getView(R.id.recycler);
                recyclerView.setLayoutManager(new LinearLayoutManager(JiChaDetailsActivity.this, LinearLayoutManager.HORIZONTAL, false));
                holder.setTextView(R.id.time, install.get(position).getStart_time());
                holder.setTextView(R.id.content, install.get(position).getContent());


                recyclerView.setAdapter(new BaseRVAdapter(JiChaDetailsActivity.this, install.get(position).getImg()) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_wuliu_img;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position2) {
                        // x.image().bind(holder.getImageView(R.id.img), install.get(position).getImg().get(position2));
                        com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(install.get(position).getImg().get(position2), holder.getImageView(R.id.img));
                    }

                });


            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.status1:
                jindu.setVisibility(View.GONE);
                max_status1.setVisibility(View.VISIBLE);
                text1.setTextColor(getResources().getColor(R.color.main_color));
                text2.setTextColor(getResources().getColor(R.color.text));
                break;
            case R.id.status2:
                jindu.setVisibility(View.VISIBLE);
                max_status1.setVisibility(View.GONE);
                text2.setTextColor(getResources().getColor(R.color.main_color));
                text1.setTextColor(getResources().getColor(R.color.text));

                break;
            case R.id.submit:
                startActivity(new Intent(this, JiChaProgressActivity.class).putExtra("orderid", getIntent().getStringExtra("orderid")));
                break;
            case R.id.jindu586:
              //  startActivity(new Intent(this, ProjectProgressActivity.class).putExtra("budget_id", bean.getInfo().getBudget_id()));
                break;
        }
    }
}
